// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import infiniteScroll from 'vue-infinite-scroll'
import axios from 'axios'
import Vuex from 'vuex'

Vue.use(infiniteScroll)
Vue.use(Vuex)

Vue.config.productionTip = false

// http response 拦截器
axios.interceptors.response.use(
  response => {
    return response;
  },
  error => {
    if (error.response) {
      if (error.response.status == 401) {
        alert("请先登录！！！！！！！")
      }
    }
    return Promise.reject(error.response.data)  // 返回接口返回的错误信息
  }
);

const store = new Vuex.Store({
  state: {
    nickName: '',
    cartCount: 0
  },
  mutations: {
    updateNickName(state, nickName) {
      state.nickName = nickName
      window.localStorage.setItem("nickName", JSON.stringify(state.nickName));
    },
    setCartCount(state, cartCount) {
      state.cartCount = cartCount
      window.localStorage.setItem("cartCount", JSON.stringify(state.cartCount));
    },
    increaseCartCount(state) {
      state.cartCount++
      window.localStorage.setItem("cartCount", JSON.stringify(state.cartCount));
    },
    decreaseCartCount(state) {
      state.cartCount--
      window.localStorage.setItem("cartCount", JSON.stringify(state.cartCount));
    }
  }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  store,        //将store注册进vue实例里，这样Vue所有的子组件都可以拥有store对象
  router,
  components: {App},
  template: '<App/>'
})
